package leetcode.median;

public class $415_AddStrings {
    public String addStrings(String num1, String num2) {
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        StringBuilder sb = new StringBuilder();

        int level = 0;

        while (i >= 0 || j >= 0 || level > 0) {
            int a = i >= 0 ? num1.charAt(i--) - '0' : 0;
            int b = j >= 0 ? num2.charAt(j--) - '0' : 0;
            int sum = a + b + level;

            if (sum > 9) {
                sb.insert(0, sum % 10);
                level = sum / 10;
            } else {
                sb.insert(0, sum);
                level = 0;
            }
        }
        return sb.toString();
    }
}
